if (SERVER) then
	require( "datastream" )
	QueueTable = {}
	AddCSLuaFile("client/cl_gMenu.lua")
	AddCSLuaFile("client/cl_gButton.lua")
	AddCSLuaFile("client/cl_gHud.lua")


	function AddToQueue( ply, command, args )
		if (ply:IsAdmin()) or (ply:IsSuperAdmin()) then
			local SONG = ""
			local zName = string.Explode(".mp3",args[1])
			if table.Count(QueueTable) < 10 then
				table.insert(QueueTable,args[1])
				if (#QueueTable) <= 1 then SendSongInfo() end
				for k,v in pairs( player.GetAll()) do
					datastream.StreamToClients( v, "QueueTable", QueueTable )
					v:SendLua("Stream.PlaySound(\"Grenade.Blip\")")
					v:PrintMessage( HUD_PRINTTALK,zName[1].." has been added to the Song Queue.")
				end
			else
				 ply:PrintMessage( HUD_PRINTTALK, "There are already 10 songs in the queue! Wait until another song is over before you add another!" )
			end
		else
			ply:PrintMessage( HUD_PRINTTALK, "You are not an Admin! You can't add songs Narb!" )
			ply:PrintMessage( HUD_PRINTCENTER, "You are not an Admin! You can't add songs Narb!" )
		end
	end

	function RemoveFromQueue( ply, command, args )
		if (ply:IsAdmin()) or (ply:IsSuperAdmin()) then
			for k,v in pairs(player.GetAll()) do
				v:PrintMessage( HUD_PRINTTALK,args[1].." has been removed from the Song Queue.")
			end
			for k,v in pairs(QueueTable) do
				if string.find(QueueTable[k],args[1]) then
					if k != 1 then
						table.remove(QueueTable,k)
					else
						ply:PrintMessage( HUD_PRINTTALK, "You can't remove a song that's already playing!" )
					end
				end
			end
			for k,v in pairs( player.GetAll()) do
				datastream.StreamToClients(v,"QueueTable",QueueTable)
			end
		else
			ply:PrintMessage( HUD_PRINTTALK, "You are not an Admin! You can't remove songs Narb!" )
			ply:PrintMessage( HUD_PRINTCENTER, "You are not an Admin! You can't remove songs Narb!" )
		end
	end

	function SendSongInfo()
		if QueueTable[1] != nil then
			local Explode = string.Explode("-",QueueTable[1])
			local Min = Explode[3]
			local Sec = Explode[4]
			ToSec = (Min * 60) + Sec
			game.ConsoleCommand("sv_voiceenable 0\n")
			
			for k,v in pairs( player.GetAll()) do
				datastream.StreamToClients(v,"SongName",{QueueTable[1]})
			end
		timer.Simple(ToSec + 10, function()
			table.remove(QueueTable,1)
			if QueueTable[1] != nil then
				for k,v in pairs( player.GetAll()) do
					datastream.StreamToClients(v,"QueueTable",QueueTable)
				end
			else
				game.ConsoleCommand("sv_voiceenable 1\n")
				for k,v in pairs( player.GetAll()) do
					v:SendLua("Stream.QueueWindow:Clear()")
				end
			end
			SendSongInfo()
		end)
		end
	end

	function SendMenu(ply)
		ply:SendLua("Stream.OpenMenu()")
	end

	function FirstSpawn(ply)
			ply:SendLua("Stream.OpenMenu()")
			ply:SendLua("Stream.Main:SetVisible(false)")
		if QueueTable[1] != nil then
		timer.Simple(1, function()
			datastream.StreamToClients(ply,"QueueTable",QueueTable)	
			ply:PrintMessage( HUD_PRINTTALK, "Welcome to the server! There is a song playing right now, so mics are disabled, you'll hear the next song that plays though :)" )
			ply:PrintMessage( HUD_PRINTCENTER, "Welcome to the server! There is a song playing right now, so mics are disabled, you'll hear the next song that plays though :)" )
		end)
		else
			ply:PrintMessage( HUD_PRINTTALK, "Welcome to the server! Press F4 to view the music menu!" )
			ply:PrintMessage( HUD_PRINTCENTER, "Welcome to the server! Press F4 to view the music menu!" )
		end
	end

	function AddTags()
		local tags = GetConVarString("sv_tags") or " "

		if string.find(tags,"gStreamSVN") == nil then
			RunConsoleCommand("sv_tags", tags..",gStreamSVN") 
		end
	end

	hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
	hook.Add( "ShowSpare2", "OpenMusic", SendMenu )
	concommand.Add( "gStream-Addsong", AddToQueue )
	concommand.Add( "gStream-Remove",RemoveFromQueue)
	hook.Add( "Initialize", "AddTags", AddTags )
end